#!/usr/bin/perl

require './upgrade-lib.pl';

my ($err_msg, $out, $err_no, $log_ev, $log_msg, $log_lev, $sv_mount_dev);

#&ReadParse();

$sv_mount_dev = &get_sysvol_mount_dev();

if(($sv_mount_dev eq "") || ($sv_mount_dev =~ /ram/))
{
	$log_ev = $text{'naslog_ev_sv_devram'};
	$log_msg = $text{'naslog_msg_sv_devram'};
	&nas_syslog($text{'modulename'}, 'naslog_warning', $log_ev, $log_msg);
	$err_msg = $text{'err_msg_sv_devram'};
	goto ERROR_OUT;
}

&ReadParseMime();

open(UPLOAD, ">$config{'uploadfile'}");
print UPLOAD $in{'inputfile'};
close(UPLOAD);

$err_msg = &dcrypt_uploadfile();
if($err_msg)
{	
	$log_ev = $text{'naslog_ev_fileformat'};
	$log_msg = $text{'naslog_msg_fileformat'};
	&nas_syslog($text{'modulename'}, 'naslog_warning', $log_ev, $log_msg);
	goto ERROR_OUT;
}

$err_msg = &extract_uploadfile();
if($err_msg)
{
	$log_ev = $text{'naslog_ev_fileformat'};
	$log_msg = $text{'naslog_msg_fileformat'};
	&nas_syslog($text{'modulename'}, 'naslog_warning', $log_ev, $log_msg);
	goto ERROR_OUT;
}

&ez_system("cp", "-f", $config{'upgradepl'}, $config{'upgradeplcpt'});

if(-e $config{'sbinupgradepl'})
{
	&ez_system("rm", "-f", $config{'sbinupgradepl'});
}

$out = ez_system12("/sbin/ccrypt", "-d", "-K", $config{"key"}, $config{'upgradeplcpt'});
if($out)
{
	$err_msg = $text{'err_msg_fileformat'};
}

$err_no = &ez_system("/sbin/upgrade.pl");

$err_no = $err_no / 256;
if($err_no)
{
        if($err_no == $text{'err_no_oldversion'})
	{
		$err_msg = $text{'err_msg_oldversion'};
		$log_ev = $text{'naslog_ev_oldversion'};
		$log_msg = $text{'naslog_msg_oldversion'};
		&nas_syslog($text{'modulename'}, 'naslog_warning', $log_ev, $log_msg);
	}

	if($err_no == $text{'err_no_erroemid'})
	{
		$err_msg = $text{'err_msg_oemid'};
		$log_ev = $text{'naslog_ev_oemid'};
		$log_msg = $text{'naslog_msg_oemid'};
		&nas_syslog($text{'modulename'}, 'naslog_warning', $log_ev, $log_msg);
	}

	if($err_no == $text{'err_no_mountdir'})
	{
		$err_msg = $text{'err_msg_mountdir'};
		$log_ev = $text{'naslog_ev_mountdir'};
		$log_msg = $text{'naslog_msg_mountdir'};
		&nas_syslog($text{'modulename'}, 'naslog_crit', $log_ev, $log_msg);
	}

	if($err_no == $text{'err_no_tmppak'})
	{
		$err_msg = $text{'err_msg_fileformat'};
		$log_ev = $text{'naslog_ev_fileformat'};
		$log_msg = $text{'naslog_msg_fileformat'};
		&nas_syslog($text{'modulename'}, 'naslog_warning', $log_ev, $log_msg);
	}

	goto ERROR_OUT;
}

&header($text{'upgrade_succ'}, "", "index", 0, 1);
print $text{'msg_upgrade_succ'};
&footer("", $text{"upload_title"});

$log_ev = $text{'naslog_ev_upgrade_succ'};
$log_msg = $text{'naslog_msg_upgrade_succ'};
&nas_syslog($text{'modulename'}, 'naslog_info', $log_ev, $log_msg);

goto SUCC_OUT;

ERROR_OUT:
&error_setup($text{'error_title'});
&error($err_msg);


SUCC_OUT:
$log_ev = $text{'naslog_ev_reboot'};
$log_msg = $text{'naslog_msg_mountdir'};
&nas_syslog($text{'modulename'}, 'naslog_info', $log_ev, $log_msg);
$out = `shutdown -r now`;
